#!/usr/bin/python
# -*- coding: utf-8 -*-

import time
import signal
import sys
from serializer import write_serial_file
from debug import debug
from notifier import Notifier
from parser import Parser
from statsaggregator import StatsAggregator

LOG_FILE = './pykeylogger-1.2.1/logs/detailed_log/logfile.txt'

class Procrastimeter:
    '''Does all the good stuff'''

    def __init__(self):
        self.n = Notifier()
        self.sa = StatsAggregator(self.n)
        self.p = Parser(self.sa)

    def start(self):
        debug('opening log file')
        try:
            f = open(LOG_FILE, 'r')
        except IOError:
            print 'Cannot find log file: ' + LOG_FILE
            return
        debug('Starting monitoring')
        try:
            while 1:

                last_position = f.tell()
                line = f.readline()

                if not line:
                    self.n.allow_notif = True
                    time.sleep(1)
                    f.seek(last_position)
                else:
                    self.p.on_log_line(line)
        except KeyboardInterrupt:
            write_serial_file(self.sa.d)
        return

if __name__ == '__main__':
    procrastimeter = Procrastimeter()
    procrastimeter.start()
